package com.anrongtec.zcpt.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.common.CommonAdapter;
import com.anrongtec.zcpt.adapter.common.MultiItemTypeAdapter;
import com.anrongtec.zcpt.adapter.common.ViewHolder;
import com.anrongtec.zcpt.base.BaseFragment;
import com.anrongtec.zcpt.model.AppModel;
import com.anrongtec.zcpt.utils.ApkUtils;
import com.anrongtec.zcpt.widget.EmptyWrapper;
import com.anrongtec.zcpt.widget.LoadMoreWrapper;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class AllAppFragment extends BaseFragment {

    private RecyclerView rl_shopping_all_app;
    private SwipeRefreshLayout sr_shopping_all_app;

    private SwipeRefreshLayout swipe_refresh;
    private CommonAdapter<AppModel> adapter;
    //	private List<FunctionModel> functions;
    private List<AppModel> apps, newApps;
    private AppModel tempApp;

    private EmptyWrapper empty;
    private LoadMoreWrapper loadMoreWrapper;

    private static final int SPAN_COUNT = 4;

    public AllAppFragment() {
    }

    public static final int APP_REFRESH = 1;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case APP_REFRESH:
                    empty.setEmptyView(R.layout.empty_view);
                    rl_shopping_all_app.setAdapter(empty);
//                    apps.clear();
//                    apps.addAll(newApps);
                    empty.notifyDataSetChanged();
                    if (swipe_refresh != null && swipe_refresh.isRefreshing()) {
                        swipe_refresh.setRefreshing(false);
                    }
                    break;

                default:
                    break;
            }

//            tv_num.setText("我的应用共"+apps.size()+"款");

        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        initData();
        View view = inflater.inflate(R.layout.fragment_all_app, container, false);
        initView(view);
        return view;
    }

    /**
     * 初始化数据
     */
    protected void initData() {
        apps = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            AppModel model = new AppModel();
            model.setAppName("移动警务");
            model.setDescription("专为公安行业打造，实现信息移动化办公");
            model.setSize(187293723l);
            model.setAppPackName("com.s");
            model.setLocalIcon(getResources().getDrawable(R.drawable.ic_launcher));
        }
        newApps = new ArrayList<>();
    }

    private void initView(View view) {
        rl_shopping_all_app = (RecyclerView) view.findViewById(R.id.rl_shopping_all_app);
        sr_shopping_all_app = (SwipeRefreshLayout) view.findViewById(R.id.sr_shopping_all_app);
        initSwipeRefresh(view);
        initRecyclerView(view);
    }


    /**
     * 初始化recyclerview
     *
     * @param view
     */
    private void initRecyclerView(View view) {
        rl_shopping_all_app.setLayoutManager(new GridLayoutManager(getContext(), SPAN_COUNT, GridLayoutManager.VERTICAL, false));

        adapter = new CommonAdapter<AppModel>(getContext(), R.layout.item_shopping_all_app, apps) {

            @Override
            protected void convert(ViewHolder holder, AppModel t, int position) {
                holder.setImageDrawable(R.id.iv_item_shopping_all_app, t.getLocalIcon());
                holder.setText(R.id.tv_item_shopping_all_app_title, t.getAppName());
                holder.setText(R.id.tv_item_shopping_all_app_size, String.valueOf(t.getSize() / (1000 * 1000)) + "MB");
                holder.setText(R.id.tv_item_shopping_all_app_content, t.getDescription());
            }

        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view,
                                    RecyclerView.ViewHolder holder,
                                    int position) {
                ApkUtils.openAPP(getContext(), apps.get(position).getAppPackName());
            }
        });

        empty = new EmptyWrapper(adapter);
        empty.setEmptyView(R.layout.loading_view);


        rl_shopping_all_app.setAdapter(empty);
    }

    /**
     * 初始化下拉刷新
     *
     * @param view
     */
    private void initSwipeRefresh(View view) {
        swipe_refresh = (SwipeRefreshLayout) view.findViewById(R.id.sr_shopping_all_app);
        swipe_refresh.setEnabled(false);
        swipe_refresh.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipe_refresh.setRefreshing(true);
                getAppsInfo();
            }


        });
    }

    //获取数据
    private void getAppsInfo() {
        handler.sendEmptyMessageDelayed(APP_REFRESH, ApiConfig.DELAY_TIME);
    }


}
